
Imports cmplib01



Public Class frmOOBBG

    Private ptrBG As BattlegroupChit
    Private ptrGame As CampaignerGameData

    Private _actionCancelled As Boolean
    Private _IndicatorMenu As New ToolStripMenuItem("Indicators")
    Private flipTokenMenu As New ToolStripMenuItem("Flip")

    Public Shadows Sub ShowDialog(ByRef _bg As BattlegroupChit, ByRef game As CampaignerGameData)

        ptrBG = _bg
        ptrGame = game

        '// list the existing unittypes
        ListUnitTypes()

        '// list properties of existing BG
        Populate()

        '// create the popup
        BuildPopup()
        MyBase.ShowDialog()
    End Sub

    Private Sub Populate()
        Try
            tvwBG.Nodes.Clear()

            For Each mem As ChitMember In ptrBG.members
                If mem.UnitType = GroundChitClass_cgd.Unit Then
                    '// list the unit and its elements
                    Dim cmem As New CampaignerItemTreeNode
                    cmem.Item = mem
                    cmem.ItemType = CampaignerItem_cgd.Unit
                    cmem.Text = DirectCast(mem, ChitUnitTypeMember).ptrUnitType.TypeName

                    cmem.ImageIndex = imageIndexByTypeName(DirectCast(mem, ChitUnitTypeMember).ptrUnitType.TypeName)
                    cmem.SelectedImageIndex = cmem.ImageIndex

                    For Each ele As Element In DirectCast(mem, ChitUnitTypeMember).elements
                        Dim elemen As New CampaignerItemTreeNode
                        elemen.ItemType = CampaignerItem_cgd.Element
                        elemen.Text = ele.ElementTypeName

                        If ele.Count > 1 Then
                            elemen.Text = ele.Count & "x " & ele.ElementTypeName & " [" & ele.ElementBaseCost & "]"
                        Else
                            elemen.Text = ele.ElementTypeName & " [" & ele.ElementBaseCost & "]"
                        End If

                        cmem.Nodes.Add(elemen)
                    Next
                    tvwBG.Nodes.Add(cmem)
                End If

            Next

            '// bg properties
            picIcon.Image = ptrBG.icon
            txtBGName.Text = ptrBG.Name
            If ptrBG.overridePointValue = 0 Then
                CalculatePV()
            Else
                chkUseOverride.Checked = True
                numOverridePV.Value = ptrBG.overridePointValue
            End If

        Catch ex As Exception
            HandleException("frmOOBG.Populate", ex)
        End Try
    End Sub

    Private Function imageIndexByTypeName(ByVal name As String) As Integer
        Try
            Dim c As System.Windows.Forms.ListBox.ObjectCollection = lstUnitTypes.Items

            Dim i As Integer
            For Each ut As UnitType In c
                i += 1
                If ut.TypeName = name Then
                    Return i
                End If
            Next

            Return 0
        Catch ex As Exception
            HandleException(c_Module, ex)
            Return 0
        End Try
    End Function

    Public ReadOnly Property ActionCancelled() As Boolean
        Get
            Return _actionCancelled
        End Get
    End Property


    Private Sub ListUnitTypes()
        Try

            For Each unt As UnitType In ptrGame.UnitTypes
                lstUnitTypes.Items.Add(unt)
                '// add the icon to the imagelist. yah!
                imlIcons.Images.Add(unt.Icon(0))
            Next

            For Each key As String In ptrGame.TokenTypes.Keys
                Dim tok As TokenType = ptrGame.TokenTypes.Item(key)
                lstUnitTypes.Items.Add(tok)
                imlIcons.Images.Add(tok.FrontProperties.icon)
            Next

        Catch ex As Exception
            HandleException("frmOOBBG.ListUnitTypes", ex)
        End Try
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        _actionCancelled = False
        Try

            If picIcon.Image Is Nothing Then
                MessageBox.Show("You must set an icon for the Battlegroup before closing this window.", "Select Icon")
                Exit Sub
            End If

            '// now add all the units to the provided BG
            If tvwBG.Nodes.Count = 0 Then _actionCancelled = True

            '// we clear the old members out to make way fer da new uns.
            ptrBG.members.Clear()

            Dim cnode As CampaignerItemTreeNode
            For Each cnode In tvwBG.Nodes
                If cnode.ItemType = CampaignerItem_cgd.Unit Then
                    ptrBG.members.add(cnode.Item)
                End If
            Next

            ptrBG.Name = txtBGName.Text

            If chkUseOverride.Checked Then
                ptrBG.overridePointValue = numOverridePV.Value
            Else
                ptrBG.overridePointValue = 0
            End If

            ptrBG.calculateProperties()

        Catch ex As Exception
            HandleException("frmOOBBG.OK", ex)
        End Try

        Me.Hide()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _actionCancelled = True
        Me.Hide()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If lstUnitTypes.SelectedItem Is Nothing Then Exit Sub

        If TypeOf lstUnitTypes.SelectedItem Is UnitType Then
            AddUnitTypeChit()
        ElseIf TypeOf lstUnitTypes.SelectedItem Is TokenType Then
            AddTokenTypeChit()
        End If

    End Sub

    Private Sub AddTokenTypeChit()
        Try
            Dim mem As New ChitTokenTypeMember
            mem.ptrTokenType = lstUnitTypes.SelectedItem
            mem.Name = mem.ptrTokenType.tokenTypeName
            mem.OwningFaction = ptrGame.selectedFaction
            mem.OwningPlayer = ptrGame.selectedPlayer

            For Each rut As RuleType In mem.ptrTokenType.TokenRules
                Dim ru As New Rule
                ru.ptrRule = rut
                ru.usesRemaining = 1
                mem.chitRules.add(ru)
            Next

            '// Add indicators!! Whoohoo!
            If Not mem.ptrTokenType.Indicators Is Nothing Then
                For Each k As String In mem.ptrTokenType.Indicators.Keys
                    Dim indi As Indicator = mem.ptrTokenType.Indicators.Item(k)
                    Select Case indi.IndicatorType
                        Case IndicatorType_enum.value
                            Dim vindicator As New ValueIndicatorEntry
                            vindicator.ptrIndicator = indi
                            vindicator.Value = DirectCast(indi, ValueIndicator).defValue

                            If mem.Indicators Is Nothing Then mem.Indicators = New Collection

                            If Not mem.Indicators.Contains(indi.IndicatorName) Then
                                mem.Indicators.Add(vindicator, indi.IndicatorName)
                            Else
                                MessageBox.Show("You already have an indicator named " & indi.IndicatorName & ".", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                            End If

                        Case IndicatorType_enum.named
                            Dim nindicator As New NamedIndicatorEntry
                            nindicator.ptrIndicator = indi
                            nindicator.name = DirectCast(indi, NamedIndicator).names.Item(1) '// default to the first item

                            If mem.Indicators Is Nothing Then mem.Indicators = New Collection

                            If Not mem.Indicators.Contains(indi.IndicatorName) Then
                                mem.Indicators.Add(nindicator, indi.IndicatorName)
                            Else
                                MessageBox.Show("You already have an indicator named " & indi.IndicatorName & ".", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                            End If

                        Case IndicatorType_enum.namedvalue
                            Dim nvindicator As New NamedValueIndicatorEntry
                            nvindicator.ptrIndicator = indi

                            '// this is to set the default
                            Dim defkey As String = ""
                            For Each dk As String In DirectCast(indi, NamedValueIndicator).Items.Keys
                                defkey = dk
                                Exit For
                            Next
                            nvindicator.Name = DirectCast(indi, NamedValueIndicator).Items.Item(defkey)

                            If mem.Indicators Is Nothing Then mem.Indicators = New Collection

                            If Not mem.Indicators.Contains(indi.IndicatorName) Then
                                mem.Indicators.Add(nvindicator, indi.IndicatorName)
                            Else
                                MessageBox.Show("You already have an indicator named " & indi.IndicatorName & ".", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                            End If
                    End Select
                Next

            End If

            Dim cnode As New CampaignerItemTreeNode
            cnode.Text = mem.Name
            cnode.Item = mem
            cnode.ItemType = CampaignerItem_cgd.Token
            tvwBG.Nodes.Add(cnode)
            cnode.ImageIndex = lstUnitTypes.SelectedIndex + 1
            cnode.SelectedImageIndex = lstUnitTypes.SelectedIndex + 1
            CalculatePV()

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub AddUnitTypeChit()
        Try
            Dim mem As New ChitUnitTypeMember
            mem.ptrUnitType = lstUnitTypes.SelectedItem
            mem.Name = mem.ptrUnitType.TypeName
            mem.OwningFaction = ptrGame.selectedFaction

            For Each rut As RuleType In mem.ptrUnitType.GCRules
                Dim ru As New Rule
                ru.ptrRule = rut
                ru.usesRemaining = 1
                mem.chitRules.add(ru)
            Next

            '// Add indicators!! Whoohoo!
            If Not mem.ptrUnitType.Indicators Is Nothing Then
                For Each k As String In mem.ptrUnitType.Indicators.Keys
                    Dim indi As Indicator = mem.ptrUnitType.Indicators.Item(k)
                    Select Case indi.IndicatorType
                        Case IndicatorType_enum.value
                            Dim vindicator As New ValueIndicatorEntry
                            vindicator.ptrIndicator = indi
                            vindicator.Value = DirectCast(indi, ValueIndicator).defValue

                            If mem.Indicators Is Nothing Then mem.Indicators = New Collection

                            If Not mem.Indicators.Contains(indi.IndicatorName) Then
                                mem.Indicators.Add(vindicator, indi.IndicatorName)
                            Else
                                MessageBox.Show("You already have an indicator named " & indi.IndicatorName & ".", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                            End If

                        Case IndicatorType_enum.named
                            Dim nindicator As New NamedIndicatorEntry
                            nindicator.ptrIndicator = indi
                            nindicator.name = DirectCast(indi, NamedIndicator).names.Item(1) '// default to the first item

                            If mem.Indicators Is Nothing Then mem.Indicators = New Collection

                            If Not mem.Indicators.Contains(indi.IndicatorName) Then
                                mem.Indicators.Add(nindicator, indi.IndicatorName)
                            Else
                                MessageBox.Show("You already have an indicator named " & indi.IndicatorName & ".", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                            End If

                        Case IndicatorType_enum.namedvalue
                            Dim nvindicator As New NamedValueIndicatorEntry
                            nvindicator.ptrIndicator = indi

                            '// this is to set the default
                            Dim defkey As String = ""
                            For Each dk As String In DirectCast(indi, NamedValueIndicator).Items.Keys
                                defkey = dk
                                Exit For
                            Next
                            nvindicator.Name = DirectCast(indi, NamedValueIndicator).Items.Item(defkey)

                            If mem.Indicators Is Nothing Then mem.Indicators = New Collection

                            If Not mem.Indicators.Contains(indi.IndicatorName) Then
                                mem.Indicators.Add(nvindicator, indi.IndicatorName)
                            Else
                                MessageBox.Show("You already have an indicator named " & indi.IndicatorName & ".", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                            End If
                    End Select
                Next

            End If
            
            '// create a treeview item
            Dim cnode As New CampaignerItemTreeNode
            cnode.Text = mem.Name
            cnode.Item = mem
            cnode.ItemType = CampaignerItem_cgd.Unit
            tvwBG.Nodes.Add(cnode)
            cnode.ImageIndex = lstUnitTypes.SelectedIndex + 1
            cnode.SelectedImageIndex = lstUnitTypes.SelectedIndex + 1
            CalculatePV()

        Catch ex As Exception
            HandleException("frmOOBG.Add", ex)
        End Try
    End Sub

    Private Sub lstUnitTypes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstUnitTypes.SelectedIndexChanged
        If lstUnitTypes.SelectedItem Is Nothing Then Exit Sub

        '// you can't override the point value of tokens
        If TypeOf lstUnitTypes.SelectedItem Is UnitType Then
            numOverridePV.Value = DirectCast(lstUnitTypes.SelectedItem, UnitType).SuggestedPointValue
            chkUseOverride.Enabled = True
        ElseIf TypeOf lstUnitTypes.SelectedItem Is TokenType Then
            chkUseOverride.Enabled = False
            chkUseOverride.Checked = False
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If tvwBG.SelectedNode Is Nothing Then Exit Sub
        DeleteSelectedItem()
    End Sub

    Private Sub DeleteSelectedItem()
        If DirectCast(tvwBG.SelectedNode, CampaignerItemTreeNode).ItemType = CampaignerItem_cgd.Element Then
            Dim no As TreeNode = tvwBG.SelectedNode.Parent
            Dim cno As CampaignerItemTreeNode = DirectCast(no, CampaignerItemTreeNode)
            DirectCast(cno.Item, ChitUnitTypeMember).elements.Remove(DirectCast(tvwBG.SelectedNode, CampaignerItemTreeNode).Item)
        End If
        tvwBG.Nodes.Remove(tvwBG.SelectedNode)

        CalculatePV()
    End Sub

    Private Function CalculatePV() As Integer
        Dim pv As Integer = 0
        Dim cnode As CampaignerItemTreeNode

        Try
            For Each cnode In tvwBG.Nodes
                If cnode.ItemType = CampaignerItem_cgd.Unit Then
                    pv += DirectCast(cnode.Item, ChitUnitTypeMember).ptrUnitType.SuggestedPointValue
                    For Each ele As GroupableElement In DirectCast(cnode.Item, ChitUnitTypeMember).elements
                        pv += ele.Count * ele.ElementBaseCost
                    Next
                ElseIf cnode.ItemType = CampaignerItem_cgd.Token Then
                    pv += DirectCast(cnode.Item, ChitTokenTypeMember).PointValue ' automatically gets the front or flipped point value. Jam!
                End If

            Next

            lblBGPointValue.Text = "BG Point Value: " & pv

            Return pv
        Catch ex As Exception
            HandleException("frmOOBG.CalculatePV", ex)
        End Try

    End Function

    Private Sub picIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picIcon.Click
        SetIcon(ptrBG.icon)
        picIcon.Image = ptrBG.icon
    End Sub


    Private Sub SetIcon(ByRef ico As Bitmap)
        Dim path As String
        Dim fDiag As New OpenFileDialog()

        fDiag.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"

        fDiag.ShowDialog()
        path = fDiag.FileName
        If path = "" Then Exit Sub
        Try
            ico = Bitmap.FromFile(path)
        Catch ex As Exception
            MsgBox("Could not load image. Please check to make sure you have a valid image file.", MsgBoxStyle.Critical, "Could Not Load Image")
        End Try
    End Sub


    Private Sub BuildPopup()

        Try
            For Each elemc As ElementClass In ptrGame.ElementMan.DefinedElementClasses
                Dim classitem As New ToolStripMenuItem(elemc.ElementClassName)
                '// add all of the items in this menu type
                For Each eleT As ElementType In ptrGame.ElementMan.DefinedElementTypes
                    If eleT.ElementClassName = elemc.ElementClassName Then

                        Dim menitem As New ToolStripMenuItem(eleT.ElementTypeName & " [" & eleT.ElementPointValue & "]")
                        menitem.Tag = eleT
                        classitem.DropDownItems.Add(menitem)
                        '// bind the event handler
                        AddHandler menitem.Click, AddressOf ClickElementMenu

                    End If
                Next

                'mnuMain.MenuItems.Add(menitem)
                ctxMenu.Items.Add(classitem)
            Next

            '// add the "add multiple elements" bit
            ctxMenu.Items.Add("-")
            Dim menuitem As New ToolStripMenuItem("Add Multiple Elements")
            AddHandler menuitem.Click, AddressOf ClickMassEditElementMenu
            ctxMenu.Items.Add(menuitem)

            ctxMenu.Items.Add("-")
            ctxMenu.Items.Add(_IndicatorMenu)
            _IndicatorMenu.Visible = False


        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub AddIndicatorMenuOptions()
        Try
            Dim cnode As CampaignerItemTreeNode = tvwBG.SelectedNode

            Select Case cnode.ItemType
                Case CampaignerItem_cgd.Token
                    Dim cutm As ChitTokenTypeMember = cnode.Item
                    _IndicatorMenu.DropDownItems.Clear()

                    Try
                        If Not cutm.Indicators Is Nothing Then
                            If cutm.Indicators.Count > 0 Then


                                For Each indie As IIndicatorEntry In cutm.Indicators
                                    'Dim indi As IIindciator = cutm.ptrUnitType.Indicators.Item(kay)
                                    'Dim indiE As IIndicatorEntry = cutm.Indicators.Item(kay)

                                    Dim indiMenuItem As New ToolStripMenuItem(indie.ptrIndicator.IndicatorName)
                                    indiMenuItem.Tag = indie

                                    Select Case indie.ptrIndicator.IndicatorType
                                        Case IndicatorType_enum.named
                                            Dim nindicator As NamedIndicatorEntry = indie
                                            indiMenuItem.Text = nindicator.ptrIndicator.IndicatorName & " [" & nindicator.name & "]"
                                            For Each n As String In DirectCast(nindicator.ptrIndicator, NamedIndicator).names

                                                '// create a new menuitem
                                                Dim nameMenu As New ToolStripMenuItem(n, Nothing, AddressOf NameChangeMenuPressed)
                                                nameMenu.Tag = indie
                                                indiMenuItem.DropDownItems.Add(nameMenu)
                                            Next

                                        Case IndicatorType_enum.namedvalue
                                            Dim nvindi As NamedValueIndicatorEntry = indie
                                            indiMenuItem.Text = nvindi.ptrIndicator.IndicatorName & "[" & nvindi.Name & "]"
                                            For Each n As String In DirectCast(nvindi.ptrIndicator, NamedValueIndicator).Items.Keys
                                                '// new menuitem
                                                Dim nameMenu As New ToolStripMenuItem(n, Nothing, AddressOf NameValueChangePressed)
                                                nameMenu.Tag = indie
                                                indiMenuItem.DropDownItems.Add(nameMenu)

                                            Next

                                        Case IndicatorType_enum.value
                                            Dim valueChange As New ToolStripMenuItem("Change Value", Nothing, AddressOf ValueChangeMenuPressed)
                                            Dim vindi As ValueIndicatorEntry = indie
                                            indiMenuItem.Text = vindi.ptrIndicator.IndicatorName & " (" & vindi.Value & ")"
                                            valueChange.Tag = indie

                                            indiMenuItem.DropDownItems.Add(valueChange)

                                    End Select
                                    '// add the item
                                    _IndicatorMenu.DropDownItems.Add(indiMenuItem)

                                Next

                            End If
                        End If
                    Catch ex As Exception

                    End Try

                Case CampaignerItem_cgd.Unit
                    Dim cutm As ChitUnitTypeMember = cnode.Item
                    _IndicatorMenu.DropDownItems.Clear()
                    Try

                        If Not cutm.Indicators Is Nothing Then
                            If Not cutm.Indicators.Count > 0 Then
                                '// this must be in the CHIT TYPE, not the unit type. lame lame lame

                                'MessageBox.Show("this is testing. currently pointing to ptrUnitType or ptrTokenType, not the actual instance of the chit.", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                                For Each indie As IIndicatorEntry In cutm.Indicators
                                    'Dim indi As IIindciator = cutm.ptrUnitType.Indicators.Item(kay)
                                    'Dim indiE As IIndicatorEntry = cutm.Indicators.Item(kay)

                                    Dim indiMenuItem As New ToolStripMenuItem(indie.ptrIndicator.IndicatorName)
                                    indiMenuItem.Tag = indie

                                    Select Case indie.ptrIndicator.IndicatorType
                                        Case IndicatorType_enum.named
                                            Dim nindicator As NamedIndicatorEntry = indie
                                            indiMenuItem.Text = nindicator.ptrIndicator.IndicatorName & " [" & nindicator.name & "]"
                                            For Each n As String In DirectCast(nindicator.ptrIndicator, NamedIndicator).names

                                                '// create a new menuitem
                                                Dim nameMenu As New ToolStripMenuItem(n, Nothing, AddressOf NameChangeMenuPressed)
                                                nameMenu.Tag = indie
                                                indiMenuItem.DropDownItems.Add(nameMenu)
                                            Next

                                        Case IndicatorType_enum.namedvalue
                                            Dim nvindi As NamedValueIndicatorEntry = indie
                                            indiMenuItem.Text = nvindi.ptrIndicator.IndicatorName & "[" & nvindi.Name & "]"
                                            For Each n As String In DirectCast(nvindi.ptrIndicator, NamedValueIndicator).Items.Keys
                                                '// new menuitem
                                                Dim nameMenu As New ToolStripMenuItem(n, Nothing, AddressOf NameValueChangePressed)
                                                nameMenu.Tag = indie
                                                indiMenuItem.DropDownItems.Add(nameMenu)

                                            Next

                                        Case IndicatorType_enum.value
                                            Dim valueChange As New ToolStripMenuItem("Change Value", Nothing, AddressOf ValueChangeMenuPressed)
                                            Dim vindi As ValueIndicatorEntry = indie
                                            indiMenuItem.Text = vindi.ptrIndicator.IndicatorName & " (" & vindi.Value & ")"
                                            valueChange.Tag = indie

                                            indiMenuItem.DropDownItems.Add(valueChange)

                                    End Select
                                    '// add the item
                                    _IndicatorMenu.DropDownItems.Add(indiMenuItem)

                                Next
                            End If
                        End If
                    Catch ex As Exception
                        HandleException(c_Module, ex)
                    End Try
            End Select

            If _IndicatorMenu.DropDownItems.Count > 0 Then
                _IndicatorMenu.Visible = True
            Else
                _IndicatorMenu.Visible = False
            End If

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub NameValueChangePressed(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim tsi As ToolStripMenuItem = sender
            Dim nvindi As NamedValueIndicatorEntry = tsi.Tag
            nvindi.Name = tsi.Text

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub ValueChangeMenuPressed(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim vindi As ValueIndicatorEntry = DirectCast(sender, ToolStripMenuItem).Tag
            Dim min, max, cur As Integer
            min = DirectCast(vindi.ptrIndicator, ValueIndicator).Minimum
            max = DirectCast(vindi.ptrIndicator, ValueIndicator).Maximum
            cur = vindi.Value

            Dim val As Integer
            Try
                val = InputBox("Enter the new value, between " & min & " and " & max & ". Current Value: " & cur, "Enter Value", cur)
                vindi.Value = val
            Catch ex As Exception
                MessageBox.Show("You must enter a numeric value", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub NameChangeMenuPressed(ByVal sender As Object, ByVal e As EventArgs)
        Try
            '// passes in the name of new value, as well as the indicator?
            Dim tsi As ToolStripMenuItem = sender

            Dim nindi As NamedIndicatorEntry = tsi.Tag
            nindi.name = tsi.Text

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub ClickMassEditElementMenu(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim cnode As CampaignerItemTreeNode

            Select Case DirectCast(tvwBG.SelectedNode, CampaignerItemTreeNode).ItemType
                Case CampaignerItem_cgd.Element
                    '// add the new element to the node above
                    cnode = tvwBG.SelectedNode.Parent
                Case CampaignerItem_cgd.Unit
                    cnode = tvwBG.SelectedNode
            End Select

            If cnode Is Nothing Then Exit Sub

            Dim f As New frmElementPicker(ptrGame, DirectCast(cnode.Item, ChitUnitTypeMember))
            f.ShowDialog()

            Dim mem As ChitUnitTypeMember = DirectCast(cnode.Item, ChitUnitTypeMember)
            '// add the elements to the selected node... i guess
            For Each ele As GroupableElement In mem.elements
                Dim elenode As New CampaignerItemTreeNode
                'Dim ET As GroupableElement= DirectCast(sender, ToolStripMenuItem).Tag
                elenode.Item = ele
                elenode.ItemType = CampaignerItem_cgd.Element
                If ele.Count > 1 Then
                    elenode.Text = ele.Count & "x " & ele.ElementTypeName & " [" & ele.ElementBaseCost & "]"
                Else
                    elenode.Text = ele.ElementTypeName & " [" & ele.ElementBaseCost & "]"
                End If
                elenode.ImageIndex = 0
                elenode.SelectedImageIndex = 0

                cnode.Nodes.Add(elenode)
            Next
            '// repopulate and calculate
            'Populate()
            CalculatePV()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub ClickElementMenu(ByVal sender As Object, ByVal e As System.EventArgs)
        '// here, we add the element to the elements! Yah!
        Try
            Dim cnode As CampaignerItemTreeNode

            Select Case DirectCast(tvwBG.SelectedNode, CampaignerItemTreeNode).ItemType
                Case CampaignerItem_cgd.Element
                    '// add the new element to the node above
                    cnode = tvwBG.SelectedNode.Parent
                Case CampaignerItem_cgd.Unit
                    cnode = tvwBG.SelectedNode

                Case CampaignerItem_cgd.Token
                    MessageBox.Show("yarp")

            End Select

            If cnode Is Nothing Then Exit Sub

            Dim ele As New GroupableElement(DirectCast(sender, ToolStripMenuItem).Tag)

            'DirectCast(cnode.Item, ChitUnitTypeMember).elements.Add(DirectCast(sender, ToolStripMenuItem).Tag)

            Dim elenode As New CampaignerItemTreeNode
            'Dim ET As GroupableElement= DirectCast(sender, ToolStripMenuItem).Tag
            elenode.Item = ele
            elenode.ItemType = CampaignerItem_cgd.Element
            elenode.Text = ele.ElementTypeName & " [" & ele.ElementBaseCost & "]"
            elenode.ImageIndex = 0
            elenode.SelectedImageIndex = 0

            '// actually add the element to the chitmember
            DirectCast(cnode.Item, ChitUnitTypeMember).elements.Add(ele)

            cnode.Nodes.Add(elenode)
            CalculatePV()

        Catch ex As Exception
            HandleException("frmOOBG.ClickElementMenu", ex)
        End Try
    End Sub

    Private Sub tvwBG_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tvwBG.KeyUp
        If tvwBG.SelectedNode Is Nothing Then Exit Sub

        Try
            '// just WHACK it.
            If e.KeyCode = Keys.Delete Then
                DeleteSelectedItem()
            End If

        Catch ex As Exception
        End Try
    End Sub

    Private Sub tvwBG_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvwBG.NodeMouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If tvwBG.SelectedNode Is Nothing Then Exit Sub
            Dim cnode As CampaignerItemTreeNode = tvwBG.SelectedNode

            If cnode.ItemType = CampaignerItem_cgd.Unit Then
                AddIndicatorMenuOptions()

                ctxMenu.Show(tvwBG, e.X, e.Y)
            ElseIf cnode.ItemType = CampaignerItem_cgd.Token Then
                '// you can flip the token right here?
                AddIndicatorMenuOptions()
                '// add token option (flip)

                ctxMenu.Show(tvwBG, e.X, e.Y)
            End If

        End If
    End Sub

    Private Sub chkUseOverride_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkUseOverride.CheckedChanged
        If chkUseOverride.Checked = True Then
            numOverridePV.Enabled = True
        Else
            numOverridePV.Enabled = False
        End If
    End Sub

End Class